 package server.shops;


import client.MapleCharacter;
import client.MapleClient;
import client.inventory.Item;
import client.inventory.ItemLoader;
import client.inventory.MapleInventoryType;
import com.alibaba.druid.pool.DruidPooledConnection;
import constants.ItemConstants;
import database.DatabaseConnection;
import handling.channel.ChannelServer;
import handling.world.WorldFindService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import server.maps.MapleMap;
import server.maps.MapleMapObject;
import server.maps.MapleMapObjectType;
import tools.Pair;
import tools.packet.PlayerShopPacket;

import java.lang.ref.WeakReference;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;



























 public abstract class AbstractPlayerStore extends MapleMapObject implements IMaplePlayerShop {
/*  30 */   private static final Logger log = LogManager.getLogger(AbstractPlayerStore.class);
   protected final String ownerName;
   protected final String pass;
   protected final int ownerId;
   protected final int owneraccount;
   protected final int itemId;
   protected final int channel;
   protected final int map;
/*  38 */   protected final AtomicLong meso = new AtomicLong(0L);
   protected final WeakReference<MapleCharacter>[] chrs;
/*  40 */   protected final Map<String, VisitorInfo> visitorsList = new HashMap<>();
/*  41 */   protected final List<BoughtItem> bought = new LinkedList<>();
/*  42 */   protected final List<MaplePlayerShopItem> items = new LinkedList<>();
/*  43 */   private final List<Pair<String, Byte>> messages = new LinkedList<>(); protected boolean open = false;
   protected boolean available = false;
   protected String des;

   public AbstractPlayerStore(MapleCharacter owner, int itemId, String desc, String pass, int slots) {
/*  48 */     setPosition(owner.getTruePosition());
/*  49 */     this.ownerName = owner.getName();
/*  50 */     this.ownerId = owner.getId();
/*  51 */     this.owneraccount = owner.getAccountID();
/*  52 */     this.itemId = itemId;
/*  53 */     this.des = desc;
/*  54 */     this.pass = pass;
/*  55 */     this.map = owner.getMapId();
/*  56 */     this.channel = owner.getClient().getChannel();
/*  57 */     this.chrs = (WeakReference<MapleCharacter>[])new WeakReference[slots];
/*  58 */     for (int i = 0; i < this.chrs.length; i++) {
/*  59 */       this.chrs[i] = new WeakReference<>(null);
     }
/*  61 */     this.visitorsList.clear();
   }


   public int getMaxSize() {
/*  66 */     return this.chrs.length + 1;
   }


   public int getSize() {
/*  71 */     return (getFreeSlot() == -1) ? getMaxSize() : getFreeSlot();
   }


   public void broadcastToVisitors(byte[] packet) {
/*  76 */     broadcastToVisitors(packet, true);
   }

   public void broadcastToVisitors(byte[] packet, boolean owner) {
/*  80 */     for (WeakReference<MapleCharacter> chr : this.chrs) {
/*  81 */       if (chr != null && chr.get() != null) {
/*  82 */         ((MapleCharacter)chr.get()).getClient().announce(packet);
       }
     }
/*  85 */     if (getShopType() != 1 && owner && getMCOwner() != null) {
/*  86 */       getMCOwner().getClient().announce(packet);
     }
   }

   public void broadcastToVisitors(byte[] packet, int exception) {
/*  91 */     for (WeakReference<MapleCharacter> chr : this.chrs) {
/*  92 */       if (chr != null && chr.get() != null && getVisitorSlot(chr.get()) != exception) {
/*  93 */         ((MapleCharacter)chr.get()).getClient().announce(packet);
       }
     }
/*  96 */     if (getShopType() != 1 && getMCOwner() != null) {
/*  97 */       getMCOwner().getClient().announce(packet);
     }
   }


   public long getMeso() {
/* 103 */     return this.meso.get();
   }


   public void setMeso(long meso) {
/* 108 */     this.meso.set(meso);
   }


   public boolean isOpen() {
/* 113 */     return this.open;
   }


   public void setOpen(boolean open) {
/* 118 */     this.open = open;
   }


   public boolean saveItems() {
/* 123 */     if (getShopType() != 1) {
/* 124 */       return false;
     }
/* 126 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 127 */       PreparedStatement ps = con.prepareStatement("DELETE FROM hiredmerch WHERE characterid = ?");
/* 128 */       ps.setInt(1, this.ownerId);
/* 129 */       ps.executeUpdate();
/* 130 */       ps.close();
/* 131 */       ps = con.prepareStatement("INSERT INTO hiredmerch (characterid, accountid, Mesos, map, channel, time) VALUES (?, ?, ?, ?, ?, ?)", 1);
/* 132 */       ps.setInt(1, this.ownerId);
/* 133 */       ps.setInt(2, this.owneraccount);
/* 134 */       ps.setLong(3, this.meso.get());
/* 135 */       ps.setInt(4, this.map);
/* 136 */       ps.setInt(5, this.channel);
/* 137 */       ps.setLong(6, System.currentTimeMillis());
/* 138 */       ps.executeUpdate();
/* 139 */       ResultSet rs = ps.getGeneratedKeys();
/* 140 */       if (!rs.next()) {
/* 141 */         rs.close();
/* 142 */         ps.close();
/* 143 */         log.info("[SaveItems] 保存雇佣商店信息出错 - 1");
/* 144 */         throw new RuntimeException("保存雇佣商店信息出错.");
       }
/* 146 */       rs.close();
/* 147 */       ps.close();

/* 149 */       List<Pair<Item, MapleInventoryType>> itemsWithType = new ArrayList<>();

/* 151 */       for (MaplePlayerShopItem pItems : this.items) {
/* 152 */         if (pItems.item == null || pItems.bundles <= 0) {
           continue;
         }
/* 155 */         if (pItems.item.getQuantity() <= 0 && !ItemConstants.isRechargable(pItems.item.getItemId())) {
           continue;
         }
/* 158 */         Item item = pItems.item.copy();
/* 159 */         item.setQuantity((short)(item.getQuantity() * pItems.bundles));
/* 160 */         itemsWithType.add(new Pair(item, ItemConstants.getInventoryType(item.getItemId())));
       }
/* 162 */       ItemLoader.雇佣道具.saveItems(con, itemsWithType, this.ownerId);
/* 163 */       return true;
/* 164 */     } catch (SQLException se) {
/* 165 */       log.info("[SaveItems] 保存雇佣商店信息出错 - 2 " + se);

/* 167 */       return false;
     }
   }
   public MapleCharacter getVisitor(int num) {
/* 171 */     return this.chrs[num].get();
   }


   public void update() {
/* 176 */     if (isAvailable()) {
/* 177 */       if (getShopType() == 1) {
/* 178 */         getMap().broadcastMessage(PlayerShopPacket.updateHiredMerchant(this));
/* 179 */       } else if (getMCOwner() != null) {
/* 180 */         getMap().broadcastMessage(PlayerShopPacket.sendPlayerBox(getMCOwner()));
       }
     }
   }


   public void addVisitor(MapleCharacter visitor) {
/* 187 */     int i = getFreeSlot();
/* 188 */     if (i > 0) {
/* 189 */       if (getShopType() >= 3) {
/* 190 */         broadcastToVisitors(PlayerShopPacket.getMiniGameNewVisitor(visitor, i, (MapleMiniGame)this));
       } else {
/* 192 */         broadcastToVisitors(PlayerShopPacket.playerInterVisitorAdd(visitor, i));
       }
/* 194 */       this.chrs[i - 1] = new WeakReference<>(visitor);
/* 195 */       updateVisitorsList(visitor, false);
/* 196 */       if (i == 6) {
/* 197 */         update();
       }
     }
   }




   public boolean isInVisitorsList(String visitorName) {
/* 206 */     return this.visitorsList.containsKey(visitorName);
   }





   public void updateVisitorsList(MapleCharacter visitor, boolean leave) {
/* 214 */     if (visitor != null && !isOwner(visitor) && !visitor.isGM()) {
/* 215 */       if (this.visitorsList.containsKey(visitor.getName())) {
/* 216 */         if (leave) {
/* 217 */           ((VisitorInfo)this.visitorsList.get(visitor.getName())).updateInTime();
         } else {
/* 219 */           ((VisitorInfo)this.visitorsList.get(visitor.getName())).updateStartTime();
         }
       } else {
/* 222 */         this.visitorsList.put(visitor.getName(), new VisitorInfo());
       }
     }
   }




   public void removeVisitorsList(String visitorName) {
/* 231 */     if (this.visitorsList.containsKey(visitorName)) {
/* 232 */       this.visitorsList.remove(visitorName);
     }
   }


   public void removeVisitor(MapleCharacter visitor) {
/* 238 */     byte slot = getVisitorSlot(visitor);
/* 239 */     boolean shouldUpdate = (getFreeSlot() == -1);
/* 240 */     if (slot > 0) {
/* 241 */       broadcastToVisitors(PlayerShopPacket.playerInterVisitorLeave(slot), slot);
/* 242 */       this.chrs[slot - 1] = new WeakReference<>(null);
/* 243 */       if (shouldUpdate) {
/* 244 */         update();
       }
/* 246 */       updateVisitorsList(visitor, true);
     }
   }


   public byte getVisitorSlot(MapleCharacter visitor) {
/* 252 */     for (byte i = 0; i < this.chrs.length; i = (byte)(i + 1)) {
/* 253 */       if (this.chrs[i] != null && this.chrs[i].get() != null && ((MapleCharacter)this.chrs[i].get()).getId() == visitor.getId()) {
/* 254 */         return (byte)(i + 1);
       }
     }
/* 257 */     if (visitor.getId() == this.ownerId) {
/* 258 */       return 0;
     }
/* 260 */     return -1;
   }


   public void removeAllVisitors(int error, int type) {
/* 265 */     for (int i = 0; i < this.chrs.length; i++) {
/* 266 */       MapleCharacter visitor = getVisitor(i);
/* 267 */       if (visitor != null) {
/* 268 */         if (type != -1) {
/* 269 */           visitor.getClient().announce(PlayerShopPacket.shopErrorMessage(error, i + 1));
         }
/* 271 */         broadcastToVisitors(PlayerShopPacket.playerInterVisitorLeave(getVisitorSlot(visitor)), getVisitorSlot(visitor));
/* 272 */         visitor.setPlayerShop(null);
/* 273 */         this.chrs[i] = new WeakReference<>(null);
/* 274 */         updateVisitorsList(visitor, true);
       }
     }
/* 277 */     update();
   }


   public String getOwnerName() {
/* 282 */     return this.ownerName;
   }


   public int getOwnerId() {
/* 287 */     return this.ownerId;
   }


   public int getOwnerAccId() {
/* 292 */     return this.owneraccount;
   }


   public String getDescription() {
/* 297 */     if (this.des == null) {
/* 298 */       return "";
     }
/* 300 */     return this.des;
   }


   public void setDescription(String desc) {
/* 305 */     if (this.des.equalsIgnoreCase(desc)) {
       return;
     }
/* 308 */     this.des = desc;
/* 309 */     if (isAvailable() && getShopType() == 1) {
/* 310 */       getMap().broadcastMessage(PlayerShopPacket.updateHiredMerchant(this, false));
     }
   }


   public List<Pair<Byte, MapleCharacter>> getVisitors() {
/* 316 */     List<Pair<Byte, MapleCharacter>> chrz = new LinkedList<>();
/* 317 */     for (byte i = 0; i < this.chrs.length; i = (byte)(i + 1)) {
/* 318 */       if (this.chrs[i] != null && this.chrs[i].get() != null) {
/* 319 */         chrz.add(new Pair(Byte.valueOf((byte)(i + 1)), this.chrs[i].get()));
       }
     }
/* 322 */     return chrz;
   }


   public List<MaplePlayerShopItem> getItems() {
/* 327 */     return this.items;
   }


   public void addItem(MaplePlayerShopItem item) {
/* 332 */     this.items.add(item);
   }


   public boolean removeItem(int item) {
/* 337 */     return false;
   }


   public void removeFromSlot(int slot) {
/* 342 */     this.items.remove(slot);
   }


   public byte getFreeSlot() {
/* 347 */     for (byte i = 0; i < this.chrs.length; i = (byte)(i + 1)) {
/* 348 */       if (this.chrs[i] == null || this.chrs[i].get() == null) {
/* 349 */         return (byte)(i + 1);
       }
     }
/* 352 */     return -1;
   }


   public int getItemId() {
/* 357 */     return this.itemId;
   }


   public boolean isOwner(MapleCharacter chr) {
/* 362 */     return (chr.getId() == this.ownerId && chr.getName().equals(this.ownerName));
   }


   public String getPassword() {
/* 367 */     if (this.pass == null) {
/* 368 */       return "";
     }
/* 370 */     return this.pass;
   }



   public void sendDestroyData(MapleClient client) {}



   public void sendSpawnData(MapleClient client) {}


   public MapleMapObjectType getType() {
/* 383 */     return MapleMapObjectType.SHOP;
   }

   public MapleCharacter getMCOwnerWorld() {
/* 387 */     int ourChannel = WorldFindService.getInstance().findChannel(this.ownerId);
/* 388 */     if (ourChannel <= 0) {
/* 389 */       return null;
     }
/* 391 */     return ChannelServer.getInstance(ourChannel).getPlayerStorage().getCharacterById(this.ownerId);
   }

   public MapleCharacter getMCOwnerChannel() {
/* 395 */     return ChannelServer.getInstance(this.channel).getPlayerStorage().getCharacterById(this.ownerId);
   }

   public MapleCharacter getMCOwner() {
/* 399 */     return getMap().getCharacterById(this.ownerId);
   }

   public MapleMap getMap() {
/* 403 */     return ChannelServer.getInstance(this.channel).getMapFactory().getMap(this.map);
   }


   public int getGameType() {
/* 408 */     if (getShopType() == 1)
/* 409 */       return 6; 
/* 410 */     if (getShopType() == 2)
/* 411 */       return 5; 
/* 412 */     if (getShopType() == 3)
/* 413 */       return 1; 
/* 414 */     if (getShopType() == 4) {
/* 415 */       return 2;
     }
/* 417 */     return 0;
   }


   public boolean isAvailable() {
/* 422 */     return this.available;
   }


   public void setAvailable(boolean b) {
/* 427 */     this.available = b;
   }


   public List<BoughtItem> getBoughtItems() {
/* 432 */     return this.bought;
   }


   public List<Pair<String, Byte>> getMessages() {
/* 437 */     return this.messages;
   }


   public int getMapId() {
/* 442 */     return this.map;
   }


   public int getChannel() {
/* 447 */     return this.channel;
   }

   public static final class BoughtItem
   {
     public final int id;
     public final int quantity;
     public final long totalPrice;
     public final String buyer;

     public BoughtItem(int id, int quantity, long totalPrice, String buyer) {
/* 458 */       this.id = id;
/* 459 */       this.quantity = quantity;
/* 460 */       this.totalPrice = totalPrice;
/* 461 */       this.buyer = buyer;
     }
   }





   public static final class VisitorInfo
   {
/* 471 */     public int inTime = 0;
/* 472 */     public long startTime = System.currentTimeMillis();


     public void updateInTime() {
/* 476 */       int time = (int)(System.currentTimeMillis() - this.startTime);
/* 477 */       if (time > 0) {
/* 478 */         this.inTime += time;
       }
     }


     public int getInTime() {
/* 484 */       return this.inTime;
     }

     public void updateStartTime() {
/* 488 */       this.startTime = System.currentTimeMillis();
     }
   }
 }


/* Location:              C:\PGX\Server.jar!\server\shops\AbstractPlayerStore.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */